﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.Entity;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace HdipCricketClub.Models
{
    [MetadataType(typeof(OpponentOtherFixtureDetailMetaData))]
    public partial class OpponentOtherFixtureDetail
    {
        CricketClubEntities db = new CricketClubEntities();

        public void UpdateOpponentOtherFixtureDetails(FormCollection scorecard, Fixture fixture)
        {
            OpponentOtherFixtureDetail currentOpponentOtherDetails = db.OpponentOtherFixtureDetails.Where(oofd => oofd.fixture_id == fixture.fixture_id).Single();
            
            string opntByes = scorecard.GetValues("OpponentOtherFixtureDetail.byes").Single();
            string opntLegByes = scorecard.GetValues("OpponentOtherFixtureDetail.leg_byes").Single();
            string opntWides = scorecard.GetValues("OpponentOtherFixtureDetail.wides").Single();
            string opntNoBalls = scorecard.GetValues("OpponentOtherFixtureDetail.no_balls").Single();
            
            if (opntByes != "")
            {
                currentOpponentOtherDetails.byes = double.Parse(opntByes);
            }

            if (opntLegByes != "")
            {
                currentOpponentOtherDetails.leg_byes = double.Parse(opntLegByes);
            }

            if (opntWides != "")
            {
                currentOpponentOtherDetails.wides = double.Parse(opntWides);
            }

            if (opntNoBalls != "")
            {
                currentOpponentOtherDetails.no_balls = double.Parse(opntNoBalls);
            }
            
            db.ObjectStateManager.ChangeObjectState(currentOpponentOtherDetails, EntityState.Modified);
            db.SaveChanges();
        }
    }

    public class OpponentOtherFixtureDetailMetaData
    {
        [Required]
        [Display(Name = "Byes")]
        public double byes;

        [Required]
        [Display (Name = "Leag Byes")]
        public double leg_byes;

        [Required]
        [Display (Name = "Wides")]
        public double wides;
        
        [Required]
        [Display (Name = "No Balls")]
        public double no_balls;

    }
}